package com.store.app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.bean.BitmapBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.UtilsBitmap;
import com.yalantis.ucrop.CropUtils;
import com.yalantis.ucrop.UCrop;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/3/17.
 * 意见反馈页面。
 */
public class AdviceActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {
    private TextView tvShowText;//显示上传照片
    private ImageView ivAddPhoto;//点击上传图片
    private ImageView ivOne, ivTwo, ivThree, ivFour, ivFive;
    private int currentPhoto = 0;//当前上传的图片数量
    private TextView tvTitle;   //标题
    private Button btSubmit;//提交
    private List<Bitmap> bitmapList = new ArrayList<>();
    private List<String> dosList = new ArrayList<String>();
    private EditText etNumber;//手机
    private EditText etContent;//意见内容
    private final int MSG_UPLOAD_PIC_SUCCESS = 1;//上传图片成功
    private static final int FLAG_CHOOSE_IMG = 5;
    private static String localTempImageFileName = "";

    private BitmapBean bitmapBean;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                //上传图片成功,调用提交用户反馈的接口
                case MSG_UPLOAD_PIC_SUCCESS:
                    String content = etContent.getText().toString().trim();
                    String number = etNumber.getText().toString().trim();
                    Log.v("zyl","要提交的图片："+getDocIds());
                    netRequest.requestFeedBack(1, getDocIds(), number, content);
//                    new FeedBackRequest().start();
                    break;
            }
        }
    };
    private NetRequest netRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_advice);
        initView();
        netRequest = new NetRequest(this);
    }

    private void initView() {
        tvShowText = (TextView) findViewById(R.id.tv_show_text_load);
        ivAddPhoto = (ImageView) findViewById(R.id.iv_addPhoto);
        ivOne = (ImageView) findViewById(R.id.iv_one);
        ivTwo = (ImageView) findViewById(R.id.iv_two);
        ivThree = (ImageView) findViewById(R.id.iv_three);
        ivFour = (ImageView) findViewById(R.id.iv_four);
        ivFive = (ImageView) findViewById(R.id.iv_five);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("意见反馈");
        btSubmit = (Button) findViewById(R.id.bt_submit);
        etNumber = (EditText) findViewById(R.id.et_number);
        etContent = (EditText) findViewById(R.id.et_content);
        btSubmit.setOnClickListener(this);
        findViewById(R.id.public_ll_return).setOnClickListener(this);
        ivAddPhoto.setOnClickListener(this);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String login_phone = preferences.getString("login_phone", "");
        etNumber.setText(login_phone);
        if(!TextUtils.isEmpty(etNumber.getText().toString().trim())){
            etNumber.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_addPhoto:
                if (currentPhoto >= 5) {
                    Toast.makeText(AdviceActivity.this, "很抱歉,最多上传5张图片！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(
                        Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,
                        FLAG_CHOOSE_IMG);
                break;
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.bt_submit:
                String content = etContent.getText().toString().trim();
                String number = etNumber.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(AdviceActivity.this, "意见反馈内容不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(number)) {
                    Toast.makeText(AdviceActivity.this, "请输入联系方式！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (content.length() > 150) {
                    Toast.makeText(AdviceActivity.this, "意见反馈内容不能多余150个字！", Toast.LENGTH_SHORT).show();
                    return;
                }
                showProgressDialog("", "提交中", false);
                 netRequest.requestFeedBack(1, getDocIds(), number, content);
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FLAG_CHOOSE_IMG && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {
                        CropUtils.startCropActivity(uri, AdviceActivity.this, 5, 5);
                }
            }
        }

        //拿到裁剪后的图片
        if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) {
            if (data != null) {
                final Uri resultUri = UCrop.getOutput(data);
                bitmapBean = UtilsBitmap.addFilePath3(resultUri.getPath(), this);
                if (bitmapBean.getIsBm() != null) {
                    //上传图片
                    netRequest.requestUploadPic(3, bitmapBean.getIsBm());
                }
            }
        } else if (resultCode == UCrop.RESULT_ERROR) {
            Toast.makeText(AdviceActivity.this, "图片剪切失败！", Toast.LENGTH_SHORT)
                    .show();
        }

    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        Toast.makeText(AdviceActivity.this, "网络不通！", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            Toast.makeText(AdviceActivity.this, "提交成功！", Toast.LENGTH_SHORT).show();
            finish();
        } else if (requestId == 2) {

        }else if(requestId == 3){
            tvShowText.setVisibility(View.GONE);
            currentPhoto++;
            if (currentPhoto == 1) {
                ivOne.setVisibility(View.VISIBLE);
                ivOne.setImageBitmap(bitmapBean.getBitmap());
            } else if (currentPhoto == 2) {
                ivTwo.setVisibility(View.VISIBLE);
                ivTwo.setImageBitmap(bitmapBean.getBitmap());
            } else if (currentPhoto == 3) {
                ivThree.setVisibility(View.VISIBLE);
                ivThree.setImageBitmap(bitmapBean.getBitmap());
            } else if (currentPhoto == 4) {
                ivFour.setVisibility(View.VISIBLE);
                ivFour.setImageBitmap(bitmapBean.getBitmap());
            } else if (currentPhoto == 5) {
                ivFive.setVisibility(View.VISIBLE);
                ivFive.setImageBitmap(bitmapBean.getBitmap());
            }
            try{
                JSONObject object = new JSONObject(result);
                String doc_id = object.getString("doc_id");
                if(!TextUtils.isEmpty(doc_id)){
                    dosList.add(doc_id);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            Toast.makeText(AdviceActivity.this, msg, Toast.LENGTH_SHORT).show();
            finish();
        } else if (requestId == 2) {

        }else if(requestId == 3){
            Toast.makeText(AdviceActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    }



    public String getDocIds() {
        String docIds = "";
        for (int i = 0; i < dosList.size(); i++) {
            docIds += dosList.get(i) + ",";
        }
        if (!TextUtils.isEmpty(docIds)) {
            docIds = docIds.substring(0, docIds.length() - 1);
        }
        return docIds;
    }
}
